<?php

namespace app\apptest\controller;
use app\apptest\common\DbCateid;
use app\apptest\common\DbType2;

use app\apptest\model\ApptestModel;
use plugins\diy_admin_form\common\MyExcel;
use cmf\controller\AdminBaseController;
use plugins\diy_admin_form\common\Common;

class AdminApptestController extends AdminBaseController
{
    /**
     * 测试应用
     * @adminMenu(
     *     'name'   => '测试应用',
     *     'parent' => 'apptest/AdminApptest/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '测试应用',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function Index(){
        $params = $this->request->param();
        //保持查询状态
        $fields = $this->getFieldsInfo($params);
        cache('index_fields',$fields);//把字段信息存入缓存,用于数据导出
        //分页数
        $pageNumber = isset($params['page_num']) ?  intval($params['page_num']) : $this->getPageCustomNumber();
        if($pageNumber<=0){$pageNumber = 10;}
        $this->assign("page_num",$pageNumber);
        $this->setPageCustomNumber($pageNumber);
        //获取数据
        $mydatamodel = new ApptestModel();
        $listpage = $mydatamodel->getDataList($fields,'id',$pageNumber);
        
        $listpage->appends($params);
        $this->assign('list',$listpage);
        $this->assign('page', $listpage->render());//单独提取分页出来
        //汇总
        $this->assign('total',$listpage->total());
        $this->assign('max_intfield',$mydatamodel->getDataList($fields,'id',0,'max','intfield'));
$this->assign('min_intfield',$mydatamodel->getDataList($fields,'id',0,'min','intfield'));

       
        return $this->fetch();
    }
    //字段设置
    private function getFieldsInfo($params){
        $fields = [];
		//标题 text title text  text-like
		$title = isset($params['title']) ?  trim($params['title']) : '';
        $this->assign("title",$title);
        $title = Common::clearHtmlStr($title);
        $title_row = ['fieldName'=>'title','fieldValue'=>$title];
        array_push($fields,$title_row);

		//分类 list cateid list  number-equal
		$cateid = isset($params['cateid']) ?  $params['cateid'] : '-1';
        $this->assign("cateid_tree",DbCateid::getListTree($cateid,true));
        $cateid_row = ['fieldName'=>'cateid','fieldValue'=>$cateid];
        array_push($fields,$cateid_row);

		//性别 list type2 list  number-equal
		$type2 = isset($params['type2']) ?  $params['type2'] : '-1';
        $this->assign("type2_tree",DbType2::getListTree($type2,true));
        $type2_row = ['fieldName'=>'type2','fieldValue'=>$type2];
        array_push($fields,$type2_row);

		//年龄 text intfield text  number-equal
		$intfield = isset($params['intfield']) ?  trim($params['intfield']) : '';
        if($intfield > 0){
            $this->assign("intfield",$intfield);
        }
        
        $intfield_row = ['fieldName'=>'intfield','fieldValue'=>$intfield];
        array_push($fields,$intfield_row);



        return $fields;
    }
    
    /**
     * 编辑
     * @adminMenu(
     *     'name'   => '编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        $params = $this->request->param();
        $id   = isset($params['id']) ? intval($params['id']) : 0;
        if($id<=0){$this->error('ID错误!');}
        $mm = new ApptestModel();
        $row = $mm->getDataRow($id);
        //填充列表项
		$this->assign("cateid_tree",DbCateid::getListTree($row['cateid'],false));
		$this->assign("type2_tree",DbType2::getListTree($row['type2'],false));

        //填充复选框

        $this->assign('post',$row);
        return $this->fetch();
    }
    /**
     * 编辑提交
     * @adminMenu(
     *     'name'   => '编辑提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑提交',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     */
    public function editPost(){
        $params = $this->request->param();
        $id = isset($params['id']) ? intval($params['id']) : 0;
        if($id<=0){ $this->error('ID错误!');}
        unset($params['id']);
        $data = $this->checkInput($params,'edit');
        if(empty($data)){
            $this->error("数据错误!");
        }
        
        $mm = new ApptestModel();
        $result = $mm->updateDataRow($id,$data);
    
        if($result){
            $this->success('保存成功!',
                url('AdminApptest/edit', ['id' => $id]));
        }else{
            $this->error('保存失败!');
        }
    }
    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        //填充列表项
		$this->assign("cateid_tree",DbCateid::getListTree('0',false));
		$this->assign("type2_tree",DbType2::getListTree('0',false));

        //填充复选框

        return $this->fetch();
    }
    /**
     * 添加提交
     * @adminMenu(
     *     'name'   => '添加提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加提交',
     *     'param'  => ''
     * )
     */
    public function addPost(){
        $params = $this->request->param();
        $data = $this->checkInput($params,'add');
        if(empty($data)){
            $this->error("出现错误!");
        }

        $mm = new ApptestModel();
        $id =  $mm->insertDataGetId($data);
    
        if($id){
            $this->success('添加成功!',
                url('AdminApptest/edit', ['id' => $id]));
        }else{
            $this->error('添加失败!');
        }
    }
    //检测用户输入的数据
    private function checkInput($params,$type='add'){
        $data = [];
        //这里可对用户提交的数据进行判断,例如:
        //$title = isset($params['title']) ? trim($params['title']) : '';
        //if(strlen($title)<=0){
//            $this->error('标题必须填写!!');
//        }
        
        if($type === 'add'){
            //添加数据
			$data['title']		= isset($params['title']) ? $params['title'] : '';
			$data['cateid']		= isset($params['cateid']) ? $params['cateid'] : '-1';
			$data['type1']		= isset($params['type1']) ? $params['type1'] : '';
			$data['type2']		= isset($params['type2']) ? $params['type2'] : '-1';
			$data['intfield']		= isset($params['intfield']) ? $params['intfield'] : '';
			$data['field1']		= isset($params['field1']) ? $params['field1'] : '';
			$data['field2']		= isset($params['field2']) ? $params['field2'] : '';

        }else{
            //编辑数据
			$data['title']		= isset($params['title']) ? $params['title'] : '';
			$data['cateid']		= isset($params['cateid']) ? $params['cateid'] : '-1';
			$data['type1']		= isset($params['type1']) ? $params['type1'] : '';
			$data['type2']		= isset($params['type2']) ? $params['type2'] : '-1';
			$data['intfield']		= isset($params['intfield']) ? $params['intfield'] : '';
			$data['field1']		= isset($params['field1']) ? $params['field1'] : '';
			$data['field2']		= isset($params['field2']) ? $params['field2'] : '';

        }
        
        return $data;
    }
    /**
     * 数据导出
     * @adminMenu(
     *     'name'   => '数据导出',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '数据导出',
     *     'param'  => ''
     * )
     */
    public function export(){
        //获取数据
        $fields = cache("index_fields");//读取缓存中的字段信息
        $mydatamodel = new ApptestModel();
        $data = $mydatamodel->getDataList($fields,'id',0);
        if(empty($data)){
            $this->error('没有可导出的数据!');
        }
        $this->exportData($data);
    }
    //数据导出,请首先在public\upload\excel_model\文件夹建立demo.xlsx文件模板
    private function exportData($data){
        $modelfilename = 'demo.xlsx';
        $outFileName = '示例表_'.date("Y-m-d-His").".xlsx";
        //在固定位置输出数据
        $fixData  = [];//['A3'=>100,'D6'=>"没有一个"];
        $dataRows = [];//循环输出的数据
        $mergeCell= [];//合并单元格,指定区域,例如 ['G4:J6','K10:R11'];
        $startRow = 3;//指定循环输出的起始行
        $startIndex =  0;//序号
        foreach ($data as $k=>$v){
            $startRow +=1;
            $startIndex +=1;
            //人员信息
            $dr = [
                'A'.$startRow    => "示例数据".$startRow,//$data['field_name'],
                'B'.$startRow    => "示例数据".$startRow,
                'C'.$startRow    => "示例数据".$startRow,
                'D'.$startRow    => "示例数据".$startRow,
            ];
            array_push($dataRows,$dr);
        }
        
        $result = MyExcel::export2excelFile($modelfilename,$outFileName,
            $fixData,$dataRows,'A4:H'.$startRow,$mergeCell);
        if(!empty($result)){
            $this->error($result);
        }
    }
    
    /**
     * 禁用或启用
     * @adminMenu(
     *     'name'   => '禁用或启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '禁用或启用',
     *     'param'  => ''
     * )
     */
    public function setflag(){
        $fieldName = "type1";
        $params = $this->request->param();
        $f = isset($params['f']) ? intval($params['f']) : -1;
        if($f>=0 && $f<=1){
            $mm = new ApptestModel();
            $ids = $this->request->param('ids/a');
            $mm->updateDataRows($ids,[$fieldName => $f]);
            $this->success("操作成功！");
        }
        $this->error('操作失败');
    }
    
    /**
     * 批量删除删除
     * @adminMenu(
     *     'name'   => '批量删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '批量删除',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function deleteRows(){
        $mm = new ApptestModel();
        $ids = $this->request->param('ids/a');
        //$result = $mm->updateDataRows($ids,['field_name' => 1]);//更新数据行,逻辑删除
        $result = $mm->deleteDataRows($ids);//物理删除数据行,无法恢复
        if($result){
            $this->success("删除成功！");
        }
        $this->error('删除失败');
    }
    //获取缓存的页行数
    private function getPageCustomNumber(){
        //自定义分页数,从缓存中读取
        $page_cache = intval(cache('page_custom_number'));
        if($page_cache <= 0){
            $this->setPageCustomNumber(10);//保存
            return 10 ;//默认每页显示10行数据
        }
        return $page_cache;
    }
    //保存页行数到缓存
    private function setPageCustomNumber($pageNumber){
        $pageNumber = intval($pageNumber);
        if($pageNumber <= 0){
            $pageNumber = 10;
        }
        cache('page_custom_number',$pageNumber);
    }

    
    
    
}